<template>
  <com-page class="">
    <u-navbar placeholder autoBack :title="$t('pages_user_address_address_msg_816760b8')" />
    
    <u-loading-page :loading="loading" loading-text="加载中..."/>
    
    <view
      v-for="(item, index) in dataList"
      :key="item.id"
      @click="onSelect(index)"
      class="com-spacing com-flex com-color-content-bg com-border-bottom"
    >
      <u-avatar :text="item.name.substr(0, 1)" class="com-mr-sm" size="32" bg-color="#eeeeee" />
      <view class="com-flex-1 com-flex-col com-mr-sm">
        <view class="">
          <text class="com-text-md com-pr-xs">{{ item.name }}</text>
          <text class="com-text-sm com-color-content">{{ item.phone }}</text>
        </view>
        <view class="com-flex com-items-start">
          <u-tag v-if="item.isDefault == 1" :text="$t('pages_user_address_address_text_18c63459')" size="mini" class="com-mr-xs" type="primary" />
          <text class="">{{ item.province }} {{ item.city }} {{ item.district }} {{ item.detailAddress }}</text>
        </view>
      </view>
      <view class="com-flex com-items-center">
        <view @click.stop="onEdit(item)" class="com-border-left com-pl-sm com-p-y-xs com-color-tips com-mr-xs">编辑</view>
        <view @click.stop="onDelete(item, index)" class="com-border-left com-pl-sm com-p-y-xs com-color-danger">删除</view>
      </view>
    </view>

    <u-empty
      v-if="dataList.length == 0"
      text="暂无收货地址,快去添加吧~"
      class="com-flex-1"
      style="padding-bottom: 100px"
    />

    <view
      @click="onAdd"
      class="com-fixed-bottom com-button block com-color-primary-bg com-round com-m-md"
      >新增地址</view
    >
  </com-page>
</template>

<script>
import { getAddressList, deleteAddress, getDefaultAddress } from '@/api/modules/user'

export default {
  name: 'Address-List',
  data() {
    return {
      dataList: [],
      loading: false,
      selectMode: false,
      selectedId: null,
    }
  },
  onLoad(options) {
    if (options && (options.mode === 'select' || options.select)) {
      this.selectMode = true
    }
    this.getList()
  },
  onShow() {
    this.getList()
  },
  methods: {
    async getList(showLoading = true) {
      try {
        if (showLoading) {
          this.loading = true
        }
        const res = await getAddressList()
        this.dataList = res || []
      } catch (error) {
        console.error('获取地址列表失败:', error)
        this.$utils.toast(this.$t('pages_user_address_address_desc_2f5ba412'))
      } finally {
        if (showLoading) {
          this.loading = false
        }
      }
    },

    onRefresh() {
      this.getList(false)
    },
    
    onSelect(index) {
      if (this.selectMode) {
        const item = this.dataList[index]
        uni.$emit('selectAddress', item)
        uni.navigateBack()
      }
    },
    
    onDelete(item, index) {
      uni.showModal({
        title: this.$t('pages_category_category_text_02d9819d'),
        content: '确定要删除该地址吗？',
        success: async (res) => {
          if (res.confirm) {
            try {
              await deleteAddress(item.id)
              this.$utils.toast(this.$t('pages_user_address_address_msg_0007d170'))
              this.dataList.splice(index, 1)
            } catch (error) {
              this.$utils.toast(this.$t('pages_user_address_address_msg_acf0664a'))
            }
          }
        }
      })
    },
    
    onEdit(item) {
      this.$utils.router('./edit?id=' + item.id)
    },
    
    onAdd() {
      this.$utils.router('./edit')
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .u-tag {
  height: unset;
  margin-top: 3px !important;
  padding: 6rpx !important;
  font-size: 22rpx;
}
</style>
